//
//  GoodsDetailViewController.m
//  StandardizedProduct
//
//  Created by 宁宁 on 16/6/27.
//  Copyright © 2016年 ym. All rights reserved.
//

#import "GoodsDetailViewController.h"

#import "GoodsDetailView.h"//商品详情View
#import "GoodsDetailTableViewCell.h"//商品详情TableViewCell
#import "GoodsRecommendTableViewCell.h"//商品推荐TableViewCell
#import "GoodsRecommendCollectionViewCell.h"//商品推荐CollectionViewCell


#import "BuyWindowView.h"//购买窗View

#import "CommitOrderViewController.h"//提交订单Controller


@interface GoodsDetailViewController ()< UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation GoodsDetailViewController
{
    GoodsDetailView *_infoView;
    
    UICollectionView *_commandCollectionView;
    UICollectionView *_goodsDetailCollectionView;
    
    CGFloat _detailCellHeight;//商品详情cell高
    CGFloat _recommandCellHeight;//推荐cell高
    
    int _numOfDetail;//商品详情图片item个数
    int _numOfReommand;//推荐商品item个数
    
    BuyWindowView *_buyWindow;//购买窗View
    
    NSMutableArray *_standardArray;//规格数组
}

 static NSString *const GoodsDetailTableViewCellID = @"GoodsDetailTableViewCell";
 static NSString *const GoodsRecommendTableViewCellID = @"GoodsRecommendTableViewCell";

 static NSString *const GoodsRecommendCollectionViewCellID = @"GoodsRecommendCollectionViewCell";

- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置状态栏颜色
    [self setStatusBar];
    
    //设置导航栏
    [self setNav];
    
    //设置cell
    [self setCell];
    
    //设置页脚
    [self setFooterView];
    
    //网络请求
    [self setNet];

    //tableview注册
    [self registerTableView];
    
    //设置商品详情View
    [self setGoodsDetailView];
    
}
#pragma mark === 设置状态栏颜色 ===
- (void)setStatusBar{
    UIView *view= [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 20)];
    view.backgroundColor = APPCOLOR;
    [self.view addSubview:view];
}
#pragma mark === 设置导航栏 ===
- (void)setNav{
    UIBarButtonItem *back = [[UIBarButtonItem alloc] init];
    back.title = @"返回";
    self.navigationItem.backBarButtonItem = back;
}
#pragma mark === 设置cell ===
- (void)setCell{
    _numOfDetail = 5;
    _numOfReommand = 5;
    _detailCellHeight = ((WIDTH - 20) / 8 * 5 + 5) * _numOfDetail + 20;
    _recommandCellHeight = (WIDTH - 20) / 3 + 20;
}
#pragma mark === 设置页脚 ===
- (void)setFooterView{
    UIButton *buyRightNow = [UIButton buttonWithType:UIButtonTypeSystem];
    buyRightNow.frame = CGRectMake(0, HEIGHT - 50, WIDTH / 2, 50);
    buyRightNow.backgroundColor = APPCOLOR;
    [buyRightNow addTarget:self action:@selector(buyRightNow) forControlEvents:UIControlEventTouchUpInside];
    buyRightNow.userInteractionEnabled = YES;
    [self.view addSubview:buyRightNow];
    
    UILabel *buy = [[UILabel alloc] initWithFrame:CGRectMake(0, HEIGHT - 50, WIDTH / 2, 50)];
    buy.text = @"立即购买";
    buy.textColor = [UIColor whiteColor];
    buy.backgroundColor = [UIColor clearColor];
    buy.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:buy];
    
    UIButton *addToCart = [UIButton buttonWithType:UIButtonTypeCustom];
    addToCart.frame = CGRectMake(WIDTH / 2, HEIGHT - 50, WIDTH / 2, 50);
    [addToCart setTitle:@"加入购物车" forState:UIControlStateNormal];
    [addToCart setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    addToCart.backgroundColor = [UIColor whiteColor];
    [addToCart addTarget:self action:@selector(addToCart) forControlEvents:UIControlEventTouchUpInside];
    addToCart.userInteractionEnabled = YES;
    [self.view addSubview:addToCart];
}

#pragma mark === 网络请求 ===
- (void)setNet{
    _standardArray = [NSMutableArray array];
    NSDictionary *dic0 = @{@"颜色":@[@"白色", @"黑色", @"灰色"]};
    NSDictionary *dic1 = @{@"尺码":@[@"170", @"165/85A", @"160/80A", @"155/70A", @"160/80A", @"155", @"170/90A", @"155", @"155", @"165/85A", @"160", @"155/70A"]};
    NSDictionary *dic2 = @{@"材质":@[@"棉麻", @"涤纶"]};
//    NSDictionary *dic3 = @{@"套餐":@[@"111111", @"222222", @"3", @"111111", @"222222", @"3", @"111111", @"222222", @"3", @"111111", @"222222", @"3"]};
    [_standardArray addObject:dic0];
    [_standardArray addObject:dic1];
    [_standardArray addObject:dic2];
//    [_standardArray addObject:dic3];
}

#pragma mark === tableview注册 ===
- (void)registerTableView{
    //注册自定义cell
    [self.tableView registerClass:[GoodsDetailTableViewCell class] forCellReuseIdentifier:GoodsDetailTableViewCellID];
    [self.tableView registerClass:[GoodsRecommendTableViewCell class] forCellReuseIdentifier:GoodsRecommendTableViewCellID];
}
#pragma maek === 设置商品详情View ===
- (void)setGoodsDetailView{
    _infoView = [[GoodsDetailView alloc] initWithFrame:CGRectMake(0, 20, WIDTH, 300 + WIDTH / 8 * 5)];
    [_infoView.back addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.tableView.tableHeaderView = _infoView;
}

#pragma mark === TableViewDelegate ===
//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}
//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:
            return _detailCellHeight;
        case 1:
            return 44;
        case 2:
            return _recommandCellHeight;
        default:
            return 0;
    }
}
//Reusable
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        GoodsDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:GoodsDetailTableViewCellID];
        _goodsDetailCollectionView = cell.collectionView;
        _goodsDetailCollectionView.delegate = self;
        _goodsDetailCollectionView.dataSource = self;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else if (indexPath.row == 2){
        GoodsRecommendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:GoodsRecommendTableViewCellID];
        _commandCollectionView = cell.collectionView;
        _commandCollectionView.delegate = self;
        _commandCollectionView.dataSource = self;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"];
    }
    cell.textLabel.text = @"相关推荐";
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

#pragma mark === CollectionViewDelegate ===
//item数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (collectionView == _goodsDetailCollectionView) {
        return _numOfDetail;
    }
    return _numOfReommand;
}
//item大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView == _goodsDetailCollectionView) {
        return CGSizeMake(WIDTH - 20, (WIDTH - 20) / 8 * 5);
    }
    return CGSizeMake((WIDTH - 20) / 3, (WIDTH - 20) / 3);
}
//Reusable
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    GoodsRecommendCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:GoodsRecommendCollectionViewCellID forIndexPath:indexPath];
    cell.imageview.backgroundColor = [UIColor colorWithRed:VALUE green:VALUE blue:VALUE alpha:1];
    return cell;
}
//当前页四周边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}
//选中某个item
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView == _goodsDetailCollectionView) {
        NSLog(@"商品详情%ld", indexPath.item);
    }else{
        NSLog(@"推荐%ld", indexPath.item);
    }
    
}

#pragma mark === 立即购买 ===
- (void)buyRightNow{
    if (!_buyWindow) {
        _buyWindow = [[BuyWindowView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        _buyWindow.standard = _standardArray;
        //刷新View
        [_buyWindow setNeedsLayout];
        
        [self.view addSubview:_buyWindow];
        
        __weak GoodsDetailViewController *goods = self;
        _buyWindow.clickBuyRightNow = ^(NSString *click, NSString *standardString){
            if ([click isEqualToString:@"0"]) {
                [goods jumpToCommitOrder];
            }else{
                [goods showAlert:standardString];
            }
        };
    }
    
    _buyWindow.frame = CGRectMake(0, HEIGHT, WIDTH, HEIGHT);
    [UIView animateWithDuration:0.4 animations:^{
        _buyWindow.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
    }];
}
//弹出警示框
- (void)showAlert:(NSString *)string{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"请选择%@", string] message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:cancle];
    [self presentViewController:alert animated:YES completion:nil];
}
//立即购买
- (void)jumpToCommitOrder{
    NSMutableArray *arr = [NSMutableArray arrayWithArray:_buyWindow.chooseStandardArray];
    [arr addObject:_buyWindow.num.text];
    NSLog(@"立即购买商品规格:%@", arr);
    
    CommitOrderViewController *commit = [[CommitOrderViewController alloc] init];
    
    commit.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:commit animated:YES];
}
#pragma mark === 加入购物车 ===
- (void)addToCart{
    NSLog(@"加入购物车");
}



- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView= [[UITableView alloc]initWithFrame:CGRectMake(0, 20, WIDTH, HEIGHT - 70) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;//去掉滑动竖条
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

@end
